package com.kk.mybatis.mapper;

import static com.kk.mybatis.mapper.MaterialDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.*;

import com.kk.mybatis.entity.Material;
import java.util.List;
import javax.annotation.Generated;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.delete.DeleteDSL;
import org.mybatis.dynamic.sql.delete.MyBatis3DeleteModelAdapter;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.select.MyBatis3SelectModelAdapter;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectDSL;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.MyBatis3UpdateModelAdapter;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.where.render.WhereClauseProvider;

@Mapper
public interface MaterialMapper {

    @InsertProvider(type=SqlProviderAdapter.class, method="insertMultiple")
    int insertMultiple(MultiRowInsertStatementProvider<Material> insertStatement);

    @Insert({
            "${insertStatement}"
    })
    @Options(useGeneratedKeys=true, keyProperty="records.id")
    int insertMultipleWithGeneratedKeys(@Param("insertStatement") String statement, @Param("records") List<Material> records);

    default int insertMultipleWithGeneratedKeys(MultiRowInsertStatementProvider<Material> multiInsert) {
        return insertMultipleWithGeneratedKeys(multiInsert.getInsertStatement(), multiInsert.getRecords());
    }

    @Select({
            "select * from material",
            "where type = #{type} and status=0"
    })
    List<Material> selectByType(int type);

    //统计该标签下的素材数量
    /*@Select({" SELECT COUNT(*) FROM material AS m, material_label_rel AS ml" ,
            " WHERE m.id=ml.m_id AND ml.l_id=#{labelId} AND m.status=0 AND ml.status=0"})*/
    @Select({"select count(*) from material_label_rel where l_id=#{labelId} and status=0"})
    int countMaterialByLabelId(int labelId);

    //分页获取素材
    @Select({
            "SELECT m.id,m.title,m.type,m.url,m.score,m.description,m.update_time FROM material_label_rel ml",
            "LEFT JOIN material m ON ml.m_id=m.id",
            "WHERE  ml.l_id=7 AND ml.status=0 AND m.status=0",
            "ORDER BY m.update_time DESC",
            "LIMIT #{startNum},#{limit}"
    })
    List<Material> selectMaterialByLabelId(@Param("labelId") int labelId,
                                           @Param("startNum") int startNum,@Param("limit") int limit);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    long count(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @DeleteProvider(type=SqlProviderAdapter.class, method="delete")
    int delete(DeleteStatementProvider deleteStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @InsertProvider(type=SqlProviderAdapter.class, method="insert")
    @Options(useGeneratedKeys=true, keyProperty="record.id")
    int insert(InsertStatementProvider<Material> insertStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @ResultMap("MaterialResult")
    Material selectOne(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @Results(id="MaterialResult", value = {
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
        @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
        @Result(column="url", property="url", jdbcType=JdbcType.VARCHAR),
        @Result(column="score", property="score", jdbcType=JdbcType.INTEGER),
        @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Material> selectMany(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @UpdateProvider(type=SqlProviderAdapter.class, method="update")
    int update(UpdateStatementProvider updateStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default QueryExpressionDSL<MyBatis3SelectModelAdapter<Long>> countByExample() {
        return SelectDSL.selectWithMapper(this::count, SqlBuilder.count())
                .from(material);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default DeleteDSL<MyBatis3DeleteModelAdapter<Integer>> deleteByExample() {
        return DeleteDSL.deleteFromWithMapper(this::delete, material);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int deleteByPrimaryKey(Integer id_) {
        return DeleteDSL.deleteFromWithMapper(this::delete, material)
                .where(id, isEqualTo(id_))
                .build()
                .execute();
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insert(Material record) {
        return insert(SqlBuilder.insert(record)
                .into(material)
                .map(id).toProperty("id")
                .map(title).toProperty("title")
                .map(type).toProperty("type")
                .map(url).toProperty("url")
                .map(score).toProperty("score")
                .map(description).toProperty("description")
                .map(status).toProperty("status")
                .map(createTime).toProperty("createTime")
                .map(updateTime).toProperty("updateTime")
                .build()
                .render(RenderingStrategy.MYBATIS3));
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insertSelective(Material record) {
        return insert(SqlBuilder.insert(record)
                .into(material)
                .map(id).toPropertyWhenPresent("id", record::getId)
                .map(title).toPropertyWhenPresent("title", record::getTitle)
                .map(type).toPropertyWhenPresent("type", record::getType)
                .map(url).toPropertyWhenPresent("url", record::getUrl)
                .map(score).toPropertyWhenPresent("score", record::getScore)
                .map(description).toPropertyWhenPresent("description", record::getDescription)
                .map(status).toPropertyWhenPresent("status", record::getStatus)
                .map(createTime).toPropertyWhenPresent("createTime", record::getCreateTime)
                .map(updateTime).toPropertyWhenPresent("updateTime", record::getUpdateTime)
                .build()
                .render(RenderingStrategy.MYBATIS3));
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default QueryExpressionDSL<MyBatis3SelectModelAdapter<List<Material>>> selectByExample() {
        return SelectDSL.selectWithMapper(this::selectMany, id, title, type, url, score, description, status, createTime, updateTime)
                .from(material);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default QueryExpressionDSL<MyBatis3SelectModelAdapter<List<Material>>> selectDistinctByExample() {
        return SelectDSL.selectDistinctWithMapper(this::selectMany, id, title, type, url, score, description, status, createTime, updateTime)
                .from(material);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Material selectByPrimaryKey(Integer id_) {
        return SelectDSL.selectWithMapper(this::selectOne, id, title, type, url, score, description, status, createTime, updateTime)
                .from(material)
                .where(id, isEqualTo(id_))
                .build()
                .execute();
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> updateByExample(Material record) {
        return UpdateDSL.updateWithMapper(this::update, material)
                .set(id).equalTo(record::getId)
                .set(title).equalTo(record::getTitle)
                .set(type).equalTo(record::getType)
                .set(url).equalTo(record::getUrl)
                .set(score).equalTo(record::getScore)
                .set(description).equalTo(record::getDescription)
                .set(status).equalTo(record::getStatus)
                .set(createTime).equalTo(record::getCreateTime)
                .set(updateTime).equalTo(record::getUpdateTime);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default UpdateDSL<MyBatis3UpdateModelAdapter<Integer>> updateByExampleSelective(Material record) {
        return UpdateDSL.updateWithMapper(this::update, material)
                .set(id).equalToWhenPresent(record::getId)
                .set(title).equalToWhenPresent(record::getTitle)
                .set(type).equalToWhenPresent(record::getType)
                .set(url).equalToWhenPresent(record::getUrl)
                .set(score).equalToWhenPresent(record::getScore)
                .set(description).equalToWhenPresent(record::getDescription)
                .set(status).equalToWhenPresent(record::getStatus)
                .set(createTime).equalToWhenPresent(record::getCreateTime)
                .set(updateTime).equalToWhenPresent(record::getUpdateTime);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKey(Material record) {
        return UpdateDSL.updateWithMapper(this::update, material)
                .set(title).equalTo(record::getTitle)
                .set(type).equalTo(record::getType)
                .set(url).equalTo(record::getUrl)
                .set(score).equalTo(record::getScore)
                .set(description).equalTo(record::getDescription)
                .set(status).equalTo(record::getStatus)
                .set(createTime).equalTo(record::getCreateTime)
                .set(updateTime).equalTo(record::getUpdateTime)
                .where(id, isEqualTo(record::getId))
                .build()
                .execute();
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKeySelective(Material record) {
        return UpdateDSL.updateWithMapper(this::update, material)
                .set(title).equalToWhenPresent(record::getTitle)
                .set(type).equalToWhenPresent(record::getType)
                .set(url).equalToWhenPresent(record::getUrl)
                .set(score).equalToWhenPresent(record::getScore)
                .set(description).equalToWhenPresent(record::getDescription)
                .set(status).equalToWhenPresent(record::getStatus)
                .set(createTime).equalToWhenPresent(record::getCreateTime)
                .set(updateTime).equalToWhenPresent(record::getUpdateTime)
                .where(id, isEqualTo(record::getId))
                .build()
                .execute();
    }

}
